//  -----------------------------------------------------------------------------------------------//
//   This file is part of SvnExternalUpdater                                                               //
//   Copyright (C)2008 Romain Verdier ( romain@codingly.com )                                      //
//                                                                                                 //
//   This program is free software; you can redistribute it and/or modify it under the terms       //
//   of the GNU General Public License as published by the Free Software Foundation; either        //
//   version 2 of the License, or (at your option) any later version. This program is distributed  //
//   in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied        //
//   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  //
//   License for more details.                                                                     //
//                                                                                                 //
//   You should have received a copy of the GNU General Public License along with this program; if //
//   not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. or  //
//   see http://www.fsf.org/licensing/licenses/gpl.html                                            //
//  -----------------------------------------------------------------------------------------------//
using System.Text.RegularExpressions;

namespace Codingly.SvnExternalUpdater.Core
{
	internal class ExternalDefinitionParser
	{
		private readonly ExternalType type;

		private string folderName;
		private bool hasRevision;
		private long revision;
		private string url;

		public ExternalDefinitionParser(ExternalType type)
		{
			this.type = type;
		}

		public string FolderName
		{
			get { return this.folderName; }
		}

		public bool HasRevision
		{
			get { return this.hasRevision; }
		}

		public long Revision
		{
			get { return this.revision; }
		}

		public ExternalType Type
		{
			get { return this.type; }
		}

		public string Url
		{
			get { return this.url; }
		}

		public bool Match(string external)
		{
			external = CleanExternal(external);
			GroupCollection groups = GetGroups(external, ExternalTypeRegex.GetRegex(this.type));
			if (groups != null)
			{
				this.folderName = groups[ExternalTypeRegex.FolderGroup].Value;
				this.url = groups[ExternalTypeRegex.UrlGroup].Value;
				this.hasRevision = groups[ExternalTypeRegex.RevisionGroup].Success;
				this.revision = this.hasRevision ? long.Parse(groups[ExternalTypeRegex.RevisionGroup].Value) : -1;
			}
			return groups != null;
		}

		private static string CleanExternal(string external)
		{
			external = external.Trim();
			external = external.Replace('\\', '/');
			return external;
		}

		private static GroupCollection GetGroups(string external, Regex regex)
		{
			GroupCollection result = null;
			Match match = regex.Match(external);
			if (match.Success)
			{
				result = match.Groups;
			}
			return result;
		}
	}
}